import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONReader;
import com.alibaba.fastjson.JSONWriter;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Test03 {
    public static void main(String[] args) throws IOException {
        Data d = new Data("2021-04-10", 1, 1);
        //将对象转换成json类型
        String jsonStr = JSON.toJSONString(d);
        System.out.println(jsonStr);
        //将json字符串转换为指定对象类型
        Data data = JSON.parseObject(jsonStr, Data.class);
        System.out.println(data);
        //将json字符串转换为对象类型
        Object obj = JSON.parseObject(jsonStr);
        System.out.println(obj);
        //取出json每一个属性的值
        String[] datas = new String[3];
        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        datas[0] = jsonObject.get("date").toString();
        datas[1] = jsonObject.getString("id");
        datas[2] = jsonObject.getString("temp");
        for (String field : datas) {
            System.out.println(field);
        }
        /**
         //序列化大型的json数据(一个数组，里面的每一个元素都是json类型)
         JSONWriter jsonWriter = new JSONWriter(new FileWriter(""));
         jsonWriter.startArray();
         //循环
         jsonWriter.writeValue(new 对象);
         //关闭
         jsonWriter.endArray();
         jsonWriter.close();
         */

        /**
         //反序列化大型json数据
         JSONReader jsonReader = new JSONReader(new FileReader(""));
         jsonReader.startArray();
         while (jsonReader.hasNext()) {
         对象 = jsonReader.readObject(对象);
         }
         //关闭
         jsonReader.endArray();
         jsonReader.close();
         */
    }
}

class Data {
    private String date;
    private int id;
    private int temp;

    public Data(String date, int id, int temp) {
        this.date = date;
        this.id = id;
        this.temp = temp;
    }

    public Data() {

    }

    @Override
    public String toString() {
        return date + "\t" + id + "\t" + temp;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getTemp() {
        return temp;
    }

    public void setTemp(int temp) {
        this.temp = temp;
    }
}